package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.entity.CityEntity;
import com.eportal.service.CityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Saber on 2017/8/10.
 */
@Controller
@RequestMapping("/admin/city")
public class CityController {

    private static Logger logger = LoggerFactory.getLogger(CityController.class.getName());

    @Resource
    private CityService cityService;

    /**
     * 市下拉列表
     * @author Saber
     * @Date 2017/8/10 上午10:21
     * @param request
     * @param province  省编码
     * @return
     */
    @RequestMapping(value = "selectCityList.json")
    @ResponseBody
    public JSONObject selectCityList(HttpServletRequest request,String province){

        JSONObject json = new JSONObject();

        try {
            List<CityEntity> list=cityService.selectCityList(province);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取市列表（下拉）异常",e);
            json.put("code",0);
            json.put("msg","获取列表异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 区下拉列表
     * @author Saber
     * @Date 2017/8/10 上午11:01
     * @param request
     * @param city      市编码
     * @return
     */
    @RequestMapping(value = "selectRegionList.json")
    @ResponseBody
    public JSONObject selectRegionList(HttpServletRequest request,String city){

        JSONObject json = new JSONObject();

        try {
            List<CityEntity> list=cityService.selectRegionList(city);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取区列表（下拉）异常",e);
            json.put("code",0);
            json.put("msg","获取列表异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
